//
//  AppDelegate.m
//  FamilyLinkPro
//
//  Created by 江鹏 on 2023/9/4.
//

#import "AppDelegate.h"
#include <execinfo.h>

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
   
    WSQQViewController *loginHomeVC = [[WSQQViewController alloc] init];
    [[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@"iv_hvac_back"]];
    [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"iv_hvac_back"]];
    HBDNavigationController *rootNav = [[HBDNavigationController alloc] initWithRootViewController:loginHomeVC];
    self.window.rootViewController = rootNav;    
    [self setupApplicationAppDelegateLaunchingWithOptions:launchOptions];
    [self _caughtException];
    
    //NSString *message = @"[哈哈]我是[嘻嘻]你是[嘻嘻嘻嘻]你是你是你是[哈哈哈哈][哈哈]";
    
  //SString *convertedString = [self convertToEmoji:message];
    
  //  NSLog(@"convertedString = %@",convertedString);
    return YES;
}




- (NSString *)convertToEmoji:(NSString *)inputString {
    
        NSDictionary *emojiMap = @{
            @"[哈哈]": @"😃",
            @"[嘻嘻]": @"😀"
            // 添加其他表情符号的映射
        };

        __block NSMutableString *convertedString = [inputString mutableCopy];

        [emojiMap enumerateKeysAndObjectsUsingBlock:^(NSString *pattern, NSString *emoji, BOOL *stop) {
            NSRange range = [convertedString rangeOfString:pattern];
            while (range.location != NSNotFound) {
                [convertedString replaceCharactersInRange:range withString:emoji];
                range = [convertedString rangeOfString:pattern];
            }
        }];

        return convertedString;

    
}

- (void)pb_backToPreviousViewController {
    
}


- (void)applicationWillResignActive:(UIApplication *)application {
    
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    
}

- (void)applicationWillTerminate:(UIApplication *)application {
    
}

#pragma mark - PUSH

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    //尽量收敛到GTNotification中实现
    //注册成功
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    //注册失败
}



#pragma mark --- setter && getter

- (UIWindow*)window
{
    if (!_window){
        _window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
        _window.backgroundColor = [UIColor whiteColor];
        [_window makeKeyAndVisible];
    }
    return _window;
}


#pragma mark --- 配置键盘操作属性
-(void)setupApplicationAppDelegateLaunchingWithOptions:(NSDictionary*)launchOptions
{
    [IQKeyboardManager sharedManager].enable = YES;
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
}


#pragma mark - CRASH

- (void)_caughtException{
    //NSexception
    NSSetUncaughtExceptionHandler(HandleNSException);
    
    //signal
    signal(SIGABRT, SignalExceptionHandler);
    signal(SIGILL, SignalExceptionHandler);
    signal(SIGSEGV, SignalExceptionHandler);
    signal(SIGFPE, SignalExceptionHandler);
    signal(SIGBUS, SignalExceptionHandler);
    signal(SIGPIPE, SignalExceptionHandler);
}

void SignalExceptionHandler(int signal){
    void* callstack[128];
    int frames = backtrace(callstack, 128);
    char **strs = backtrace_symbols(callstack, frames);
    NSMutableArray *backtrace = [NSMutableArray arrayWithCapacity:frames];
    for (int i = 0; i < frames; i++) {
        [backtrace addObject:[NSString stringWithUTF8String:strs[i]]];
    }
    free(strs);
    //存储crash信息。
}

void HandleNSException(NSException *exception){
    __unused NSString *reason = [exception reason];
    __unused NSString *name = [exception name];
    //存储crash信息。
}


@end
